package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQueryParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<GeofenceVO> selectGeo(GeofenceQuery query) {
        log.debug("查询电子围栏业务，参数：{}", query);
        List<GeofenceVO> geofenceVOS = geofenceMapper.selectGeo(query);
        for (int i = 0; i < geofenceVOS.size(); i++){
            GeofenceVO geofenceVO = geofenceVOS.get(i);
            VehicleQueryParam vehicleQueryParam = new VehicleQueryParam();
            vehicleQueryParam.setGeofenceId(geofenceVO.getId());
            vehicleQueryParam.setGeofenceBindStatus("1");
            List<VehicleVO> totalVehicles = vehicleMapper.selectVehicle(vehicleQueryParam);
            geofenceVO.setTotalNum(totalVehicles.size());
            vehicleQueryParam.setStatus("1");
            List<VehicleVO> availableVehicles = vehicleMapper.selectVehicle(vehicleQueryParam);
            geofenceVO.setAvailableNum(availableVehicles.size());
            geofenceVO.setVehicleList(availableVehicles);
            log.debug("查询结果：{}", availableVehicles);
        }
        return geofenceVOS;
    }

    @Override
    public void saveGeo(GeofenceSaveParam geo) {
        log.debug("保存电子围栏业务，参数：{}", geo);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geo, geofence);
        if (geo.getId() == null){
            log.debug("保存电子围栏业务");
            geofence.setCreateTime(new Date());
            geofence.setStatus("1");
            geofenceMapper.saveGeo(geofence);
        }else {
            log.debug("修改电子围栏业务");
            geofence.setUpdateTime(new Date());
            geofenceMapper.updateGeo(geofence);
        }
    }

    @Override
    public void deleteGeo(Long id) {
        log.debug("删除电子围栏业务，id:{}", id);
        VehicleQueryParam vehicleQueryParam = new VehicleQueryParam();
        vehicleQueryParam.setGeofenceId(id);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQueryParam);
        if (!list.isEmpty()){
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);
        }else {
            geofenceMapper.deleteGeo(id);
        }
    }
}
